﻿using Common.Library;
using Container.Library;
using Microsoft.AspNetCore.Mvc;
using Project.Logic;
using Project.Model;
using System;
using System.Collections.Generic;
using Tool.Model;
using Web.Manage.App_Start;

namespace Web.Manage.Areas.Project.Controllers
{
    /// <summary>
    /// 工程项目管理控制器
    /// </summary>
    [Area("Project")]
    public class InfoController : BaseController
    {

        private readonly IProjectService projectService = null;//工程管理逻辑
        public InfoController()
        {
            //工程管理逻辑注入
            projectService = UnityCIContainer.Instance.GetService<IProjectService>();
        }

        #region 界面

        /// <summary>
        /// 项目列表页面
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyView)]
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        ///新增界面
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult Add()
        {
            return View();
        }

        /// <summary>
        /// 编辑界面
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult Modify()
        {
            return View();
        }
        #endregion

        #region 查询操作

        /// <summary>
        /// 分页获取所有的可操作按钮
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult LoadListInfo()
        {
            var resultInfo = new ResultJsonInfo<List<ProjectInfoQueryResponse>>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<ProjectInfoQueryRequest>>();

                resultInfo = projectService.LoadProjectList(parametersInfo);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        /// 获取工程类型信息列表
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult LoadProjectTypeSelect()
        {
            var resultInfo = new ResultJsonInfo<List<EnumToSelectItem>>();
            try
            {
                resultInfo.Data = EnumToSelectItem.GetEnumDescriptionList(typeof(ProjectType), false);
                resultInfo.Code = ActionCodes.Success;
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        /// 获取承包性质信息列表
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult LoadContractSelect()
        {
            var resultInfo = new ResultJsonInfo<List<EnumToSelectItem>>();
            try
            {
                resultInfo.Data = EnumToSelectItem.GetEnumDescriptionList(typeof(ProjectContractualNature), false);
                resultInfo.Code = ActionCodes.Success;
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        /// 获取所有的工程项目
        /// </summary>
        /// <param name="parametersInfo"></param>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult LoadProjectAllList()
        {
            var resultInfo = new ResultJsonInfo<List<ProjectInfoQueryResponse>>();
            try
            {
                resultInfo = projectService.LoadProjectAllList();
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }
        #endregion

        #region 增加信息
        /// <summary>
        /// 增加信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult AddInfo()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<ProjectOperateRequest>>();

                resultInfo = projectService.Save(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }
        #endregion

        #region 修改信息
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult ModifyInfo()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<ProjectOperateRequest>>();

                resultInfo = projectService.Save(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }
        #endregion

        #region 删除信息
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult Remove()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<List<int>>>();

                resultInfo = projectService.Remove(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }
        #endregion
    }
}